package org.fcy.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Pattern;
import lombok.RequiredArgsConstructor;
import org.fcy.domain.po.Result;
import org.fcy.domain.po.User;
import org.fcy.mapper.UserMapper;
import org.fcy.service.IUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;



import static org.fcy.domain.po.Result.success;

/**
 * <p>
 * 用户基本信息表 前端控制器
 * </p>
 *
 * @author fcy
 * @since 2025-10-27
 */
@Tag(name = "用户基本信息表")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Validated
@CrossOrigin
public class UserController {
    private final IUserService userService;
    private final UserMapper userMapper;

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result<String> register(@Pattern(regexp = "^[a-zA-Z0-9_-]{3,16}$") String username, @Pattern(regexp = "^[a-zA-Z0-9_-]{6,16}$") String password) {
        //判断用户名是否存在
        QueryWrapper<org.fcy.domain.po.User> wrapper = new QueryWrapper<org.fcy.domain.po.User>()
                .eq("username",username);
        User user=userMapper.selectOne(wrapper);
        if(user!=null){
            return Result.error("用户名已存在");
       };

        userService.register(username,password);
        return success("注册成功");

    }
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "^[a-zA-Z0-9_-]{3,16}$") String username, @Pattern(regexp = "^[a-zA-Z0-9_-]{6,16}$") String password) {
        return userService.login(username,password);
    }


}
